<!--suppress ALL -->
<div class="layui-fluid layui-anim website-anim" id="website-notice" lay-title="通知公告管理"
     xmlns:th="http://www.w3.org/1999/xhtml">
    <div class="layui-row website-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body website-table-full">
                    <form class="layui-form layui-table-form" lay-filter="notice-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md10">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">标题</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="title" autocomplete="off" class="layui-input"
                                                   placeholder="请输入标题">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">类型</label>
                                        <div class="layui-input-inline">
                                            <select name="typeCode">
                                                <option value="">请选择</option>
                                                <option
                                                        th:each="item : ${noticeType}"
                                                        th:value="${item.code}" th:text="${item.name}">
                                                </option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-blue-plain table-action"
                                     id="query">
                                    查询
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-green-plain table-action"
                                     id="reset">
                                    重置
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-blue-plain table-action"
                                     id="addNotice">
                                    添加
                                </div>
                                <!--<div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
                                     shiro:hasAnyPermissions="notice:add,notice:update,notice:export">
                                    <i class="layui-icon">&#xe875;</i>
                                </div>-->
                            </div>
                        </div>
                    </form>
                    <table lay-filter="noticeTable" lay-data="{id: 'noticeTable'}"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<!--表格中字典的显示-->
<script type="text/html" id="notice-type">
    {{#
    var dict=layui.dict.getByCode(d.typeCode);
    }}
    {{#  if(dict){ }}
    <span>{{ dict.name }}</span>
    {{#  } }}
</script>
<!--表格中状态的按钮-->
<script type="text/html" id="notice-publish">
    <input type="checkbox" {{ d.isPublish ? 'checked' : '' }} value="{{d.noticeId}}" lay-filter="publish_status" lay-skin="switch" lay-text="发布|取消"/>
</script>
<!--表格中发布时间-->
<script type="text/html" id="publish-time">
    {{#  if(d.isPublish){ }}
    <span>{{ d.publishTime }}</span>
    {{#  }else { }}
    <span>未发布</span>
    {{#  } }}

</script>
<!--表格中的操作栏-->
<script type="text/html" id="notice-option">
    <span shiro:lacksPermission="notice:list,notice:update,notice:delete">
        <span class="layui-badge-dot website-bg-orange"></span> 无权限
    </span>
    <a lay-event="detail" shiro:hasPermission="notice:list" class="website-green">详情</a>
    {{#  if(!d.isPublish){ }}
    <a lay-event="edit" shiro:hasPermission="notice:update" class="website-blue">修改</a>
    {{#  } }}

<!--    <a lay-event="del" shiro:hasPermission="notice:delete"><i
            class="layui-icon website-edit-area website-red">&#xe7f9;</i></a>-->
</script>

<script data-th-inline="none" type="text/javascript">
    layui.use(['jquery', 'form', 'table', 'website', 'dict'], function () {
        var $ = layui.jquery,
            website = layui.website,
            form = layui.form,
            table = layui.table,
            dict = layui.dict,
            dropdown = layui.dropdown,
            $view = $('#website-notice'),
            $query = $view.find('#query'),
            $addNotice = $view.find('#addNotice'),
            $reset = $view.find('#reset'),
            $searchForm = $view.find('form'),
            sortObject = {field: 'createTime', type: null},
            tableIns,
            createTimeFrom,
            createTimeTo;
        form.render();
        initTable();
        /**
         * 发布按钮的点击事件
         */
        form.on('switch(publish_status)', function (obj) {
            website.post(ctx + 'notice/publish/' + this.value, {isPublish: obj.elem.checked}, function (d) {
                if(d.code==200){
                    website.alert.success('修改成功');
                    $query.click();
                }else{
                    obj.elem.checked=!obj.elem.checked;
                    form.render();
                }
            });
        });

        /**
         * 搜索按钮旁边的更多，这个是下拉控件
         **/
        /*        dropdown.render({
                    elem: $view.find('.action-more'),
                    click: function (name, elem, event) {
                        var checkStatus = table.checkStatus('noticeTable');
                        if (name === 'add') {
                            website.modal.open('新增通知公告', 'biz/notice/noticeAdd', {
                                btn: ['提交', '重置'],
                                area: $(window).width() <= 750 ? '95%' : '50%',
                                offset: '30px',
                                yes: function (index, layero) {
                                    $('#notice-add').find('#submit').trigger('click');
                                },
                                btn2: function () {
                                    $('#notice-add').find('#reset').trigger('click');
                                    return false;
                                }
                            });
                        }
                        if (name === 'delete') {
                            if (!checkStatus.data.length) {
                                website.alert.warn('请选择需要删除的通知公告');
                            } else {
                                website.modal.confirm('删除通知公告', '确定删除该通知公告？', function () {
                                    var noticeIds = [];
                                    layui.each(checkStatus.data, function (key, item) {
                                        noticeIds.push(item.noticeId)
                                    });
                                    deleteNotice(noticeIds.join(','));
                                });
                            }
                        }
                    },
                    options: [{
                        name: 'add',
                        title: '新增通知',
                        perms: 'notice:add'
                    }/!*,
                        {
                        name: 'delete',
                        title: '删除通知',
                        perms: 'notice:delete'
                    }*!/
                    ]
                });*/

        /**
         * table操作栏的按钮监听事件
         **/
        table.on('tool(noticeTable)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'detail') {
                website.modal.view('通知公告', 'biz/notice/noticeDetail/' + data.noticeId, {
                    area: $(window).width() <= 750 ? ['95%','90%'] : ['50%','80%']
                });
            }
            if (layEvent === 'del') {
                if(data.isPublish){
                    website.alert.error('请先取消发布再进行删除操作！');
                    return;
                }
                website.modal.confirm('删除通知公告', '确定删除该通知公告？', function () {
                    deleteNotice(data.noticeId);
                });
            }
            if (layEvent === 'edit') {
                if(data.isPublish){
                    website.alert.error('请先取消发布再进行删除操作！');
                    return;
                }
                website.modal.open('修改通知公告', 'biz/notice/noticeUpdate/' + data.noticeId, {
                    area: $(window).width() <= 750 ? '90%' : '50%',
                    offset: '30px',
                    btn: ['保存', '取消'],
                    yes: function (index, layero) {
                        $('#notice-update').find('#submit').trigger('click');
                    },
                    btn2: function () {
                        layer.closeAll();
                    }
                });
            }
        });

        /**
         * 表格的排序点击
         **/
        table.on('sort(noticeTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

        $query.on('click', function () {
            var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            tableIns.reload({where: params, page: {curr: 1}});
        });

        $addNotice.on('click', function () {
            website.modal.open('新增通知公告', 'biz/notice/noticeAdd', {
                btn: ['保存', '重置'],
                area: $(window).width() <= 750 ? '95%' : '50%',
                offset: '30px',
                yes: function (index, layero) {
                    $('#notice-add').find('#submit').trigger('click');
                },
                btn2: function () {
                    $('#notice-add').find('#reset').trigger('click');
                    return false;
                }
            });
        });

        $reset.on('click', function () {
            $searchForm[0].reset();
            sortObject.type = 'null';
            createTimeTo = null;
            createTimeFrom = null;
            tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
        });

        /**
         * 初始化表格
         */
        function initTable() {
            tableIns = website.table.init({
                elem: $view.find('table'),
                id: 'noticeTable',
                url: ctx + 'notice/list',
                cols: [[
                    /*{type: 'checkbox'},*/
                    /* { type: 'numbers', title: '序号', },*/
                    {field: 'title', title: '标题', minWidth: 100},
                    {field: 'year', title: '年份'},
                    {title: '类型', templet: '#notice-type'},
                    { title: '发布时间',templet: '#publish-time'},
                    {title: '发布状态', templet: '#notice-publish'},
                    {title: '操作', toolbar: '#notice-option', minWidth: 140}
                ]]
            });
        }

        /**
         * 获取查询条件
         * @returns {{}}
         */
        function getQueryParams() {
            var params = $searchForm.serializeJson();
            return params;
        }

        /**
         * 删除方法
         * @param noticeIds
         */
        /*function deleteNotice(noticeId) {
            website.post(ctx + 'notice/delete/' + noticeId, null, function () {
                website.alert.success('删除通知公告成功！');
                $query.click();
            });
        }*/
    })
</script>